package com.miracle.toolbox.file.matchAndMergeContent;

import com.miracle.toolbox.file.FileUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MatchMergeByLine {

    static final List<String> splitArr = Arrays.asList("α","β","γ","δ","ε","ζ","η","θ","ι");//42689

    public static void main(String[] args) {
        MergeByLineAndCode("F:\\ideaWorkspace\\nbms\\nbms-ebank\\nbms-ebank-website\\src\\main\\resources\\message_zh_CN.properties",
                Arrays.asList("F:\\ideaWorkspace\\nbms\\nbms-ebank\\nbms-ebank-website\\src\\main\\resources\\message_zh_CN.properties",
                        "F:\\ideaWorkspace\\nbms\\nbms-ebank\\nbms-ebank-website\\src\\main\\resources\\message_zh_TW.properties",
                        "F:\\ideaWorkspace\\nbms\\nbms-ebank\\nbms-ebank-website\\src\\main\\resources\\message_en_US.properties"),
                "C:\\Users\\zht\\Desktop\\match.txt");
    }

    /**
     * 基于基准文件，将其他文件可以匹配的行追加到基准行后面
     * （用于乱序的key、value内容的匹配）
     * *无法作为公用工具，需要更具实际情况改写
     *
     */
    public static void MergeByLineAndCode(String basePath, List<String> sourcePaths, String resultPath){
        //1.解析基础数据
        List<String> baseDatas = getBaseData(basePath);
        baseDatas.stream().forEach(e-> System.out.println(e));
        System.out.println("------------------------------------------");

        //2.解析带匹配文件
        List<List<String>> resultDatas = new ArrayList<>();
        resultDatas.add(baseDatas);
        for (int i = 0; i < sourcePaths.size(); i++) {
            String sourcePath = sourcePaths.get(i);
            List<String> resultData = matchDataWithOrder(baseDatas, sourcePath);
            resultData.stream().forEach(e-> System.out.println(e));
            System.out.println("------------------------------------------");
            resultDatas.add(resultData);
        }

        //3.所有数据写入文件
        writeList2File(resultDatas, resultPath);
        System.out.println("------------------------------------------");

    }

    public static void writeList2File(List<List<String>> resultDatas, String resultPath){

        File file = FileUtils.getFileWithCreate(resultPath, FileUtils.IS_FILE);
        FileWriter fileWriter = null;
        try {
            try {
                fileWriter = new FileWriter(resultPath);
            } catch (IOException e) {
                e.printStackTrace();
            }

            for (int i = 0; i < resultDatas.get(0).size(); i++) {
                String data = "";
                for (int j = 0; j < resultDatas.size(); j++) {
                    data += resultDatas.get(j).get(i);
                    if (j<resultDatas.size()-1) data += splitArr.get(j);
                }
                try {
                    System.out.println(data);
                    fileWriter.write(data + "\r\n");
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        } finally {
            if (fileWriter!=null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    public static List<String> matchDataWithOrder(List<String> baseDatas, String sourcePath){

        ArrayList<String> resultData = new ArrayList<>(baseDatas);
        List<String> sourceDataOrg = getDataByFile(sourcePath);

        for (int i = 0; i < baseDatas.size(); i++) {
            String baseData = baseDatas.get(i);


            for (int j = 0; j < sourceDataOrg.size(); j++) {
                String line = sourceDataOrg.get(j);
                if (line.indexOf(baseData)>-1){
                    // String data = getData(line, j + 1);
                    int idx = line.indexOf("=");
                    if (idx<0) {
                        System.out.println("第"+ (j+1) + "行未获取到有效数据："+line);
                        continue;
                    }
                    String data = line.substring(line.indexOf("=")+1).replaceAll("#","").trim();
                    // resultData.set(i,resultData.get(i)+split+data);
                    resultData.set(i, data);
                    break;
                }
                if (j==sourceDataOrg.size()-1){
                    // resultData.set(i,resultData.get(i)+split+"");
                    resultData.set(i,"");
                }
            }


        }
        return resultData;



    }

    public static List<String> getBaseData(String basePath){
        File baseFile = new File(basePath);
        List<String> baseData = new ArrayList<>();
        List<String> baseDataOrg = getDataByFile(basePath);

        for (int i = 0; i < baseDataOrg.size(); i++) {

        }
        for (int i = 0; i < baseDataOrg.size(); i++) {
            String line = baseDataOrg.get(i);

            //如果不是可以直接使用的数据，需要在这里处理
            String dataLine = getData(line, i + 1);

            if (dataLine.length()>0) baseData.add(dataLine);
        }

        return baseData;
    }
    public static String getData(String line, int lineNum){
        int idx = line.indexOf("=");
        if (idx<0) {
            System.out.println("第"+ lineNum + "行未获取到有效数据："+line);
            return "";
        }
        String dataLine = line.substring(0, line.indexOf("=")).replaceAll("#","").trim();
        return dataLine;
    }

    public static List<String> getDataByFile(String filePath){
        List<String> dataOrg = new ArrayList<>();
        try {
            dataOrg = Files.readAllLines(new File(filePath).toPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataOrg;
    }
}
